package com.boot.dog.rest.core;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.dog.common.auth.DogUser;
import com.boot.dog.common.annotation.SysLog;
import com.boot.dog.common.util.ApiResult;
import com.boot.dog.common.util.Result;
import com.boot.dog.common.util.SecurityUtil;
import com.boot.dog.entities.ao.LockAo;
import com.boot.dog.entities.ao.UserAo;
import com.boot.dog.entities.dto.UserAreaDto;
import com.boot.dog.entities.dto.UserDto;
import com.boot.dog.entities.dto.UserPasswordDto;
import com.boot.dog.entities.pojo.User;
import com.boot.dog.entities.vo.UserVo;
import com.boot.dog.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author: MaxLv
 * @Date: 2020/1/13
 */
@RestController
@RequestMapping("/core/user")
@AllArgsConstructor
public class UserRest {

    private final UserService userService;

    @GetMapping("/getUsers")
    @ApiOperation(value = "用户分页查询")
    public ApiResult<IPage<UserVo>> getUsers(Page page, UserAo ao) {
        return userService.getUsers(page, ao);
    }

    @PostMapping("/saveUser")
    @SysLog(desc = "添加用户")
    @ApiOperation(value = "添加用户")
    public ApiResult<Void> saveUser(@Valid @RequestBody UserDto dto) {
        User user = new User();
        BeanUtil.copyProperties(dto, user);
        return userService.saveUser(user, dto.getRoleIds());
    }

    @PostMapping("/editUser")
    @SysLog(desc = "编辑用户")
    @ApiOperation(value = "编辑用户")
    public ApiResult<Void> editUser(@Valid @RequestBody UserDto dto) {
        User user = new User();
        BeanUtil.copyProperties(dto, user);
        return userService.editUser(user, dto.getRoleIds());
    }

    @DeleteMapping("/delById/{id}")
    @SysLog(desc = "删除用户")
    @ApiOperation(value = "删除用户")
    public ApiResult<Void> delById(@PathVariable Integer id) {
        return userService.delById(id);
    }

    @GetMapping("/getCurrUser")
    @ApiOperation(value = "获取当前用户")
    public ApiResult<User> getCurrUser() {
        DogUser authUser = SecurityUtil.getUser();
        User user = userService.findByUsername(authUser.getUsername());
        return Result.of(user);
    }

    @PostMapping("/unlock")
    @ApiOperation(value = "解锁")
    public ApiResult<Void> unLock(@RequestBody LockAo ao) {
        DogUser authUser = SecurityUtil.getUser();
        User user = userService.findUserByUsername(authUser.getUsername());
        if (user == null) {
            return Result.warn("用户不存在");
        }
        if (!new BCryptPasswordEncoder().matches(ao.getPassword(), user.getPassword())) {
            return Result.warn("密码错误");
        }
        return Result.ok("解锁成功");
    }

    @PostMapping("/modifyPass")
    @ApiOperation(value = "修改密码")
    @SysLog(desc = "修改密码")
    public ApiResult<Void> modifyPass(@RequestBody UserPasswordDto dto) {
        return userService.modifyPass(dto.getOldPassword(), dto.getNewPassword());
    }

    @PostMapping("/enableOrDisableUser")
    @SysLog(desc = "启用/禁用用户")
    @ApiOperation(value = "启用/禁用用户")
    public ApiResult<Void> enableOrDisableUser(@RequestBody User user) {
        return userService.enableOrDisableUser(user);
    }

    @PostMapping("/editUserArea")
    @SysLog(desc = "分配区域权限")
    @ApiOperation(value = "分配区域权限")
    public ApiResult<Void> editUserArea(@RequestBody UserAreaDto dto) {
        return userService.editUserArea(dto);
    }
}
